package com.dotstone.chipism.util;

import java.util.ArrayList;
import java.util.List;

import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.Status;

import android.os.Handler;
import android.util.Log;

public class CheckStatusUtil {

	public List<Status> statuss = new ArrayList<Status>();
	public List<Status> statuss1 = new ArrayList<Status>();
	public List<Status> statuss2 = new ArrayList<Status>();
	private List<Device> devices = new ArrayList<Device>();
	public static Handler mHandler;
	public static CheckStatusUtil mCheckManager;
	private int time = 0;

	public CheckStatusUtil() {

	}

	public static CheckStatusUtil getInstance() {
		if (mCheckManager == null) {
			mCheckManager = new CheckStatusUtil();// 单例对象实例
		}
		return mCheckManager;
	}

	private Object readResolve() {
		return getInstance();
	}

	public Handler getmHandler() {
		return mHandler;
	}

	// 根据网关的position查询状态
	public void check1(int groupPos) {
		Log.e("wmy", "CheckStatusUtil check1");
		devices.clear();
		statuss.clear();
		time = 0;
		devices = DeviceManager.getInstance().getWlans().get(groupPos).getChildDevices();
		for (int i = 0; i < devices.size(); i++) {
			Status status = new Status();
			status.setDeviceId(devices.get(i).getDeviceID());
			status.setNeedToCheck(true);
			status.setStatus(DeviceManager.getInstance().checkDeviceState(devices.get(i).getDeviceID()));
			statuss.add(status);
		}
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.e("wmy", "mHandler.postDelayed 69" + " " + devices.size());
				checkRec();
			}
		}, devices.size() * 1000);
	}

	public void check(int groupPos) {
		Log.e("wmy", "CheckStatusUtil check");
		devices.clear();
		statuss.clear();
		time = 0;
		switch (groupPos) {
		case 2:
			devices = DeviceManager.getInstance().getSwitchs();
			for (int i = 0; i < devices.size(); i++) {
				Status status = new Status();
				status.setDeviceId(devices.get(i).getDeviceID());
				status.setNeedToCheck(true);
				status.setStatus(DeviceManager.getInstance().checkDeviceState(devices.get(i).getDeviceID()));
				statuss.add(status);
			}
			break;
		case 3:
			devices = DeviceManager.getInstance().getSockets();
			for (int i = 0; i < devices.size(); i++) {
				Status status = new Status();
				status.setDeviceId(devices.get(i).getDeviceID());
				status.setNeedToCheck(true);
				status.setStatus(DeviceManager.getInstance().checkDeviceState(devices.get(i).getDeviceID()));
				statuss.add(status);
			}
			break;
		case 5:
			devices = DeviceManager.getInstance().getLights();
			for (int i = 0; i < devices.size(); i++) {
				Status status = new Status();
				status.setDeviceId(devices.get(i).getDeviceID());
				status.setNeedToCheck(true);
				status.setStatus(DeviceManager.getInstance().checkDeviceState(devices.get(i).getDeviceID()));
				statuss.add(status);
			}
			break;
		case 7:
			devices = DeviceManager.getInstance().getCameras();
			for (int i = 0; i < devices.size(); i++) {
				Status status = new Status();
				status.setDeviceId(devices.get(i).getDeviceID());
				status.setNeedToCheck(true);
				status.setStatus(DeviceManager.getInstance().checkDeviceState(devices.get(i).getDeviceID()));
				statuss.add(status);
			}
			break;
		case 99:// 检查全部设备
			devices.addAll(DeviceManager.getInstance().getSwitchs());
			devices.addAll(DeviceManager.getInstance().getSockets());
			devices.addAll(DeviceManager.getInstance().getLights());
			devices.addAll(DeviceManager.getInstance().getCameras());
			for (int i = 0; i < devices.size(); i++) {
				Status status = new Status();
				status.setDeviceId(devices.get(i).getDeviceID());
				status.setNeedToCheck(true);
				status.setStatus(DeviceManager.getInstance().checkDeviceState(devices.get(i).getDeviceID()));
				statuss.add(status);
			}
			break;
		default:
			break;
		}
		mHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Log.e("wmy", "mHandler.postDelayed 69" + " " + devices.size());
				checkRec();
			}
		}, devices.size() * 1000);
	}

	private void checkRec() {
		// Log.i("wmy", "延时后检查");
		time++;
		if (time > 2) {
			mHandler.sendEmptyMessage(13);
			return;
		}
		mHandler.sendEmptyMessage(14);
		int count = 0;
		for (int i = 0; i < statuss.size(); i++) {
			if (statuss.get(i).isNeedToCheck()) {
				devices.get(i).checkStatus();
				count++;
			}
		}
		Log.i("wmy", "共有count = " + count);
		if (count > 0) {
			mHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					checkRec();
				}
			}, count * 1000);
		} else {
			Log.i("wmy", "mHandler.sendEmptyMessage(13)");
			mHandler.sendEmptyMessageDelayed(13, 2*1000);
		}
	}

	public void changeRec(String id) {
		for (int i = 0; i < statuss.size(); i++) {
			if (statuss.get(i).getDeviceId().equals(id)) {
				statuss.get(i).setNeedToCheck(false);
			}
		}
	}

}
